
import BookModel from '../../models/book.js';
const bookModel = new BookModel();

import LikeModel from '../../models/like.js';
const likeModel = new LikeModel();

Page({

    /**
    * 页面的初始数据
    */
    data: {
        posting: false,
        animationData: {}
    },

    /**
    * 生命周期函数--监听页面加载
    */
    onLoad: function (options) {
        const bid = options.bid;
        wx.showLoading({
            title: '加载中',
        });
        bookModel.getBookDetail(bid).then((res) => { //书籍详情
            this.setData({
                bookDetail: res
            });
        });
        bookModel.getShortComment(bid).then((res) => {  //书籍短评
            this.setData({
                shortComment: res
            });
            wx.hideLoading();
        });
        bookModel.getBookFavor(bid).then((res) => {   //书籍点赞情况
            this.setData({
                favor: res
            });
        });
    },
    onLike(e){
        let behavior = e.detail.behavior;
        likeModel.like(behavior, this.data.favor.id, 400, (res) => {
            wx.showToast({
                title: behavior === 'like' ? '点赞成功' : '取消点赞',
                duration: 1000,
                icon: 'none'
            })
        });
    },
    onFakePost(){
        this.setData({
            posting: true
        });
        var animation = wx.createAnimation({
            duration: 500,
            timingFunction: "ease",
        });
        this.animation = animation;
        animation.opacity(1).step();
        this.setData({
            animationData: animation.export()
        });
    },
    hide(){
        this.setData({
            posting: false
        });
        this.animation.opacity(0).step();
        this.setData({
            animationData: this.animation.export()
        });
    },
    //发送短评
    onSendComment(e){
        const comment = e.detail.text || e.detail.value;
        if (!comment) return;
        if (comment && comment.length > 12){
            wx.showToast({
                title: '短评不得超多12个字符',
                icon: 'none'
            })
            return
        }
        bookModel.addShortComment(this.data.shortComment.book_id, comment).then((res) => {
            wx.showToast({
                title: '+ 1',
                icon: 'none'
            });
            this.data.shortComment.comments.unshift({
                content: comment,
                nums: 1
            });
            this.setData({
                shortComment: this.data.shortComment,
                posting: false
            });
        });
    },
    /**
    * 生命周期函数--监听页面初次渲染完成
    */
    onReady: function () {

    },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})